from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.generics import CreateAPIView, RetrieveAPIView, GenericAPIView, UpdateAPIView, ListAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jwt.views import ObtainJSONWebToken

from answers.models import Answers
from questions.models import Question
from users.models import User

# get请求，mobiles/(?P<mobile>1[3-9]\d{9})/count
from users.serializers import UserSerializer, UserInfoSerializer, UserAnswerSerializer, \
    UserShareSerializer


# class UserQuestionsView(RetrieveAPIView):
#     permission_classes = [IsAuthenticated]
#     serializer_class = UserQuestionsViewSerializer

# class UserAccountView(RetrieveAPIView, UpdateAPIView):
#     # 用于用户账号设置的修改
#     permission_classes = [IsAuthenticated]
#     serializer_class = UserAccountSerializer
#
#     def get_object(self):
#         return self.request.user


class UserMobileCount(APIView):
    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()
        # 后端不做判断，返回到前端做判断
        data = {"mobile": mobile,
                "count": count

                }
        return Response(data)


class UserView(CreateAPIView):
    """
    :param:username,password,sms_code,allow,mobile
    :return: user_id,mobile,username,status_code
    """
    # 可以使用序列化器实现创建用户模型和验证参数
    serializer_class = UserSerializer


# get 请求 url:/user
class UserBasicInfoView(RetrieveAPIView, UpdateAPIView):
    permission_classes = [IsAuthenticated]  # 需要判断用户是否登录
    serializer_class = UserInfoSerializer  # 用户输出用户的信息

    # queryset = User.objects.all()

    # def get_queryset(self):
    #     # 该方法不能传id，但可以通过self.request.user获得请求的用户对象
    #     user=User.objects.get(id=self.request.user.id)
    #     return user

    def get_object(self):  # 查询单个的数据需要重写get_object

        return self.request.user


# # 用于查用户的简历
# class UserProfileView(RetrieveAPIView, UpdateAPIView):
#     permission_classes = [IsAuthenticated]  # 需要判断用户是否登录
#     serializer_class = UserProfileSerializer  # 用户输出用户的信息
#
#     def get_object(self):  # 查询单个的数据需要重写get_object
#
#         return self.request.user
#
#     def put(self, request, *args, **kwargs):
#         serializer = self.get_serializer(data=request.data, partial=True)
#         return super().update(request, *args, **kwargs)  # post  url:/authorizations
#
#
# class VerificationSkillView(UpdateAPIView):
#     permission_classes = [IsAuthenticated]
#     serializer_class = VerificationSkillSerializer
#
#     def get_object(self):  # 查询单个的数据需要重写get_object
#
#         return self.request.user


class UserAnswerView(RetrieveAPIView, UpdateAPIView): # 回答
    permission_classes = [IsAuthenticated]
    serializer_class = UserAnswerSerializer

    def get_object(self):

        return self.request.user


class UserShareView(RetrieveAPIView, UpdateAPIView): # 回答
    permission_classes = [IsAuthenticated]
    serializer_class = UserShareSerializer

    def get_object(self):

        return self.request.user